package cn.bdqn.dao.impl;

import cn.bdqn.bean.Topic;
import cn.bdqn.bean.User;
import cn.bdqn.dao.BaseDao;
import cn.bdqn.dao.TopicDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class TopicDaoImpl extends BaseDao implements TopicDao {
    @Override
    public List<Topic> getAll() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<Topic> list=new ArrayList<>();
        try{
            connection=getConn();
            String sql="select * from topic";
            preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                Topic topic=new Topic(resultSet.getInt("tid"),resultSet.getString("tname"));
                topic.setTpic(resultSet.getString("tpic"));
                list.add(topic);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return list;
    }

    @Override
    public int del(Integer id) {
        String sql="delete from topic where tid=?";
        Object[] objects={id};
        return upd(sql,objects);
    }

    @Override
    public int updTopic(Topic topic) {
        String sql="update topic set tname=? where tid=?";
        Object[] objects={topic.getTname(),topic.getTid()};
        return upd(sql,objects);
    }

    @Override
    public int add(Topic topic) {
        String sql="insert into topic values(default,?,?)";
        Object[] objects={topic.getTname(),topic.getTpic()};
        return upd(sql,objects);
    }

    @Override
    public Topic getOne(Integer id) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Topic topic=null;
        try{
            connection=getConn();
            String sql="select * from topic where tid=?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,id);
            resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                topic=new Topic(resultSet.getInt("tid"),resultSet.getString("tname"));

            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return topic;
    }

    /**
     * 查询总条数
     * @return
     */
    @Override
    public Integer getTotalCount() {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        Integer count=0;
        try{
            connection=getConn();
            String sql="select count(1) from topic";
            preparedStatement=connection.prepareStatement(sql);
            resultSet=preparedStatement.executeQuery();
            if(resultSet.next()){
                count=resultSet.getInt(1);
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return count;
    }

    /**
     * 查询每页显示的主题集合数据
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<Topic> getTopicPage(Integer pageNo, Integer pageSize) {
        Connection connection=null;
        PreparedStatement preparedStatement=null;
        ResultSet resultSet=null;
        List<Topic> list=new ArrayList<>();
        try{
            connection=getConn();
            String sql="select * from topic limit ?,?";
            preparedStatement=connection.prepareStatement(sql);
            preparedStatement.setInt(1,(pageNo-1)*pageSize);
            preparedStatement.setInt(2,pageSize);
            resultSet=preparedStatement.executeQuery();
            while(resultSet.next()){
                Topic topic=new Topic(resultSet.getInt("tid"),
                        resultSet.getString("tname"));
                topic.setTpic(resultSet.getString("tpic"));
                list.add(topic);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            closeAll(resultSet,preparedStatement,connection);
        }
        return list;
    }
}
